package com.qfedu.boot.util;

import redis.clients.jedis.Jedis;

import java.util.Map;

public class JedisUtil {
    private static Jedis jedis;
    static {
        //1.创建Redis服务器的连接
        jedis=new Jedis("43.143.157.87",6379);
        //2.设置密码
        jedis.auth("zzjava");
    }

    //新增
    /**
     * String类型*/
    public static void addStr(String key,String val){
        jedis.set(key, val);
    }
    public static void addStr(String key,String val,long seconds){
        jedis.setex(key,seconds,val);
    }
    /**
     * Hash类型*/
    public static void addHash(String key,String field,String val){
        jedis.hset(key, field, val);
    }
    public static void addHash(String key, Map<String,String> map){
        jedis.hset(key,map);
    }
    /**
     * Geo类型*/
    public static void addGeo(String key,double lon,double lat,String address){
        jedis.geoadd(key, lon, lat, address);
    }
    //删除
    /**
     * 删除指定的key*/
    public static void delKeys(String... keys){
        jedis.del(keys);
    }
    /**
     * 删除Hash中存在的数据*/
    public static void delHash(String key,String field){
        jedis.hdel(key,field);
    }
    //查询
    /**
     * String类型*/
    public static String getStr(String key){
        return jedis.get(key);
    }
    public static Map getHash(String key){
        return jedis.hgetAll(key);
    }
    public static String getHash(String key,String field){
        return jedis.hget(key, field);
    }
    //验证
    public static boolean existsKey(String... keys){
        return jedis.exists(keys)>0;
    }
    public static boolean existsHash(String key,String field){
        return jedis.hexists(key, field);
    }
    /**
     * 设置有效期*/
    public static void expire(String key,long seconds){
        jedis.expire(key, seconds);
    }
}
